package com.timkj.zgyd.controller;

import com.timkj.zgyd.dto.GetTelephoneListDto;
import com.timkj.zgyd.entity.ResultVO;
import com.timkj.zgyd.service.ZgydUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.IOException;

/**
 * @author : liji
 * @date : 2020-09-01 15:13
 */
@Validated
@Controller
@RequestMapping("/zgyd/user")
@Api(value = "用户接口",tags = {"用户接口"})
public class ZgydUserController {
    private ZgydUserService zgydUserService;

    @Autowired
    public ZgydUserController(ZgydUserService zgydUserService) {
        this.zgydUserService = zgydUserService;
    }

    @ApiOperation(value = "获取openid")
    @RequestMapping(value = "/getOpenid",method = RequestMethod.GET)
    public void getOpenid(@RequestParam("code") String code,
                          @RequestParam("state") String state,
                          HttpServletResponse response,
                          HttpServletRequest request){
        String openid = zgydUserService.saveOpenid(code,state);
        request.getSession().setAttribute("openid",openid);
        try {
            response.sendRedirect("http://zgyd.timkj.cn/zgyd/?openid=" + openid);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ResponseBody
    @ApiOperation(value = "获取手机号列表接口")
    @RequestMapping(value = "/getTelephoneList",method = RequestMethod.POST)
    public ResultVO getTelephoneList(@RequestBody @Valid GetTelephoneListDto dto,HttpServletRequest request){
        return zgydUserService.getTelephoneList(dto,request);
    }

    @ResponseBody
    @ApiOperation(value = "模糊查询手机号")
    @RequestMapping(value = "/searchTelephoneNumber",method = RequestMethod.GET)
    public ResultVO searchTelephoneNumber(@RequestParam("openid") @Valid @NotBlank(message = "openid不能为空")  String openid,
                                          @RequestParam("number") @Valid @NotNull(message = "number不能为空") String number,
                                          HttpServletRequest request){
        return zgydUserService.searchTelephoneNumber(openid,number,request);
    }
}
